library(plyr)
library(igraph)
library(fmsb)
library(Hmisc)
library(MASS)
library(stargazer)
library(tidyverse)
library(multcomp)

library(gsynth)
data(gsynth)
library(panelView)

setwd("~/Dropbox/China Huawei Paper/Production Materials/Replication/")
master.data <- read.csv("dataFile_China_Huawei.csv"); head(master.data)


# 250k
dat <- master.data
dat$treated = 0
threshold=250000
dat = as.data.frame(dat %>%
  group_by(cabb) %>%
  dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
         treated = ifelse(china.huawei > threshold, 1, treated)) %>%
  fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy<=0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out1, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Autocracies\nSocial Media Filtering")
out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out2, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Autocracies\nInternet Shutdowns")
out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out3, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Autocracies\nSocial Media Monitoring")
out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out4, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Autocracies\nArrests for Online Content")
out4$est.avg


# 500k
dat <- master.data
dat$treated = 0
threshold=500000
dat = as.data.frame(dat %>%
  group_by(cabb) %>%
  dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
         treated = ifelse(china.huawei > threshold, 1, treated)) %>%
  fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy<=0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out1, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Autocracies\nSocial Media Filtering")
out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out2, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Autocracies\nInternet Shutdowns")
out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out3, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Autocracies\nSocial Media Monitoring")
out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out4, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Autocracies\nArrests for Online Content")
out4$est.avg


# 1 million
dat <- master.data
dat$treated = 0
threshold=1000000
dat = as.data.frame(dat %>%
  group_by(cabb) %>%
  dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
         treated = ifelse(china.huawei > threshold, 1, treated)) %>%
  fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy<=0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out1, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nSocial Media Filtering")
out1$est.avg
#dev.print(file=paste(figdir,"gsynthATTv2smgovfilprcT5.pdf",sep=""), device=pdf); dev.off()

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out2, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nInternet Shutdowns")
out2$est.avg
#dev.print(file=paste(figdir,"gsynthATTv2smgovshutT5.pdf",sep=""), device=pdf); dev.off()

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out3, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nSocial Media Monitoring")
out3$est.avg
#dev.print(file=paste(figdir,"gsynthATTv2smgovsmmonT5.pdf",sep=""), device=pdf); dev.off()

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out4, type="gap", xlim=c(-5,10), ylim=c(-0.2,1.0), main="Autocracies\nArrests for Online Content")
out4$est.avg
#dev.print(file=paste(figdir,"gsynthATTv2smarrestT5.pdf",sep=""), device=pdf); dev.off()


# 5 million
dat <- master.data
dat$treated = 0
threshold=5000000
dat = as.data.frame(dat %>%
  group_by(cabb) %>%
  dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
         treated = ifelse(china.huawei > threshold, 1, treated)) %>%
  fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy<=0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out1, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Autocracies\nSocial Media Filtering")
out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out2, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Autocracies\nInternet Shutdowns")
out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out3, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Autocracies\nSocial Media Monitoring")
out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out4, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Autocracies\nArrests for Online Content")
out4$est.avg


# 10 million
dat <- master.data
dat$treated = 0
threshold=10000000
dat = as.data.frame(dat %>%
  group_by(cabb) %>%
  dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
         treated = ifelse(china.huawei > threshold, 1, treated)) %>%
  fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy<=0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out1, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Autocracies\nSocial Media Filtering")
out1$est.avg

out2 <- gsynth(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out2, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Autocracies\nInternet Shutdowns")
out2$est.avg

out3 <- gsynth(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out3, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Autocracies\nSocial Media Monitoring")
out3$est.avg

out4 <- gsynth(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
plot(out4, type="gap", xlim=c(-3,10), ylim=c(-0.2,1.0), main="Autocracies\nArrests for Online Content")
out4$est.avg


